<template>
  <div>

    <el-form ref="searchForm"  label-width="20px">

      <div v-if="GLOBAL.global_limit.includes('36')">
        <el-col :span="2" :offset="22">
          <el-button type="primary" size="mini" @click="showAdd">新增角色</el-button>
        </el-col>
      </div>


    </el-form>
    <el-dialog title="角色信息" align="left" :visible.sync="dialogFormVisible" :show-close="false">

      <el-form :model="info" label-width="100px">
        <div v-if="condition1">
        <el-form-item label="角色名称" >
          <el-input v-model="info.name" ></el-input>
        </el-form-item>
        <el-form-item label="角色备注" >
          <el-input v-model="info.remark" ></el-input>
        </el-form-item>
        <el-form-item label="所属学校" prop="uid" v-if="uLabel">
          <el-select v-model="info.uid"  placeholder="请选择学校">
            <el-option
              v-for="item in uOptions"
              :key="item.id"
              :label="item.label"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="所属学校" prop="uid" v-if="!uLabel" >
            <el-select v-model="info.uid"  placeholder="请选择学校" :disabled="true">
              <el-option
                v-for="item in uOptions"
                :key="item.id"
                :label="item.label"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </div>
        <el-form-item label="角色权限" >
          <el-card>
            <el-tree
              :data="data"
              show-checkbox
              :default-expanded-keys="info.limit2"
              :default-checked-keys="info.limit2"
              node-key="id"
              ref="tree"
              highlight-current
              :props="defaultProps">
            </el-tree>
          </el-card>
        </el-form-item>
      </el-form>
      <div v-if="condition2">
      <div style="margin-top: 100px" slot="footer" class="dialog-footer" align="right">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click=neRole>确 定</el-button>
      </div>
      </div>
      <div v-else>
        <div style="margin-top: 100px" slot="footer" class="dialog-footer" align="right">
          <el-button type="primary" @click=close>关 闭</el-button>
        </div>
      </div>
    </el-dialog>

    <el-table
      stripe
      :row-style="{height:'45px'}"
      :cell-style="{padding:'0px'}"
      style="font-size: 14px;width: 100%"
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
      :data= roleTable >
      <el-table-column
        prop="index"
        align="center"
        label="序号"
        width="150">
      </el-table-column>
      <el-table-column
        align="center"
        prop="name"
        label="角色名称"
        width="200"
        :show-overflow-tooltip='true' >
      </el-table-column>
      <el-table-column
        align="center"
        prop="remark"
        label="角色备注"
        width="300"
        :show-overflow-tooltip='true' >
      </el-table-column>
      <el-table-column
        align="center"
        label="内容"
        width="200">
        <template slot-scope="scope">
          <span @click="more(scope.row)" >点此查看详情</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="universityName"
        label="所属学校"
        width="200"
        :show-overflow-tooltip='true' >
      </el-table-column>

      <el-table-column  align="center" label="设置">
        <template slot-scope="scope">
          <span v-if="GLOBAL.global_limit.includes('37')">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)">编辑</el-button>
          </span>
          <span v-if="GLOBAL.global_limit.includes('38')">
            <el-button
              size="mini"
              type="danger"
              @click="Delete( scope.row)">删除</el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
    export default {
        name: "Role",
        created() {
            this.initPermission();
            this.initData();
        },
        methods: {
            initPermission(){
                this.$axios.post("managementSystem/user/personalInfo").then((res) => {
                    if (res.data.errorCode === 200) {
                        let { img, name, password, phone, university, userType,college } = res.data.data
                        if(university==null || university==""){
                            //如果是超级管理员是没有学校属性的
                            this.data=this.data1;
                        }else{
                            //除了超级管理员都有学校的属性
                            this.data=this.data2;
                        }
                    }
                });
            },
            initData() {
                this.$axios.post("managementSystem/role/findAll")
                    .then(data => {
                            if (data.status != 200) {
                                this.$message.error('服务出错'+data.status);
                            } else {
                                if (data.data.errorCode == '200') {
                                    this.roleTable=data.data.data
                                    for (var i in this.roleTable) {
                                        this.roleTable[i]["index"] = parseInt(i) + 1;
                                        if(this.roleTable[i]["limit1"]==null||this.roleTable[i]["limit1"]===""){
                                            this.roleTable[i]["limit1"]=[]
                                        }else{
                                            this.roleTable[i]["limit1"]=this.roleTable[i]["limit1"].split(",")
                                        }
                                        if(this.roleTable[i]["limit2"]==null||this.roleTable[i]["limit2"]===""){
                                            this.roleTable[i]["limit2"]=[]
                                        }else{
                                            this.roleTable[i]["limit2"]=this.roleTable[i]["limit2"].split(",")
                                        }
                                    }
                                }
                            }
                    })
                this.$axios.post("managementSystem/university/getAllNameByUid")
                    .then(data => {
                        if (data.status != 200) {
                            this.$message.error('服务出错' + data.status);
                        } else {
                            if (data.data.errorCode == '200') {
                                var u = data.data.schoolNameList
                                this.uOptions=[{
                                    "id":-1,
                                    "label":"无"
                                }]
                                for (var i in u) {
                                    this.uOptions.push({"id":u[i]["id"],"label":u[i]["universityName"]})
                                }
                            }
                        }
                    })
            },
            neRole(){
                var l1=this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys())//全部选择的
                var l2=this.$refs.tree.getCheckedKeys()//只包括选中的
                this.info.limit1=l1.join(",")
                this.info.limit2=l2.join(",")
                for(var key in this.info){
                    console.log("属性：" + key + ",值：" + this.info[key]);
                }
                if(this.info.id!=null) {//编辑
                    console.log("这是编辑")
                    this.$axios.post("managementSystem/role/edit",this.info)
                        .then(data => {
                            if (data.status != 200) {
                                this.$message.error('服务出错' + data.status);
                            } else {
                                if (data.data.errorCode == '200') {
                                    this.$message({
                                        message: '编辑成功',
                                        type: 'success'
                                    });
                                }else{
                                    this.$message.error('编辑失败');
                                }
                                this.initData()
                            }
                        });
                }
                else{
                    console.log("这是新增")
                    this.$axios.post("managementSystem/role/insert",this.info)
                        .then(data => {
                            if (data.status != 200) {
                                this.$message.error('服务出错' + data.status);
                            } else {
                                if (data.data.errorCode == '200') {
                                    this.$message({
                                        message: '插入成功',
                                        type: 'success'
                                    });
                                }else{
                                    this.$message.error('插入失败');
                                }
                                this.initData()
                            }
                        });

                }
                this.dialogFormVisible = false
            },
            cancel(){
                this.dialogFormVisible = false
            },
            showAdd(){
                this.addInfo.name = ""
                this.addInfo.remark = ""
                this.addInfo.uid=null
                this.addInfo.limit1 = []
                this.addInfo.limit2 = []
                this.info=this.addInfo
                this.$nextTick(() => {
                    this.$refs.tree.setCheckedKeys([]);
                });
                this.dialogFormVisible = true
                this.uLabel=1
            },
            handleEdit(row){
                this.$nextTick(() => {
                    this.$refs.tree.setCheckedKeys(row.limit2);
                });
                this.editInfo.id = row.id
                this.editInfo.name = row.name
                this.editInfo.remark = row.remark
                this.editInfo.uid=row.uid
                this.editInfo.limit1 = row.limit1
                this.editInfo.limit2 = row.limit2
                this.info=this.editInfo
                this.dialogFormVisible = true
                this.uLabel=0
                console.log("编辑前的权限列表："+row.limit1)
            },
            Delete(row){
                this.$confirm('此操作将删除该角色, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'danger',
                    center: true
                }).then(() => {
                    this.$axios.post("managementSystem/role/delete", {"id": row.id})
                        .then(data => {
                            if (data.status != 200) {
                                this.$message.error('服务出错' + data.status);
                            } else {
                                if (data.data.errorCode == '200') {
                                    this.$message({
                                        message: '删除成功',
                                        type: 'success'
                                    });
                                } else {
                                    this.$message.error('删除失败');
                                }
                                this.initData()
                            }
                        });
                })
            },
            more(row){
                this.$nextTick(() => {
                    this.$refs.tree.setCheckedKeys(row.limit2);
                });
                this.showInfo.limit1 = row.limit1
                this.showInfo.limit2 = row.limit2
                this.info=this.showInfo
                this.dialogFormVisible = true
                this.condition1=false
                this.condition2=false
            },
            close(){
                this.condition1=true
                this.condition2=true
                this.dialogFormVisible = false
            }
        },

        data() {
            return {
                roleTable:[],
                data:[],
                //data1是给超级管理员看的
                data1: [
                    {
                    id: 1,
                    label: '运营数据',
                    children: [{
                        id: 2,
                        label: '高校推广分布',
                    }, {
                            id: 3,
                            label: '运营数据',
                        }]},
                    {
                    id: 4,
                    label: '数据报表',
                    children: [{
                        id: 47,
                        label: '生源地区分布',
                    },{
                        id: 48,
                        label: '学院人数分布',
                    },{
                        id: 49,
                        label: '评测数据统计',
                    },{
                        id: 50,
                        label: '问题学生分布',
                    }]
                }, {
                    id: 25,
                    label: '心理健康中心',
                    children: [{
                        id: 26,
                        label: '量表管理',
                        children:[{
                                id: 27,
                                label: '查询量表'
                            },
                            {
                                id: 28,
                                label: '删除量表'
                            }]
                    },{
                        id: 20,
                        label: '学生管理',
                        children:[{
                            id: 21,
                            label: '查询学生'
                        },{
                            id: 22,
                            label: '新增学生'
                        },{
                            id: 23,
                            label: '编辑学生'
                        },{
                            id: 24,
                            label: '删除学生'
                        }]
                    }, {
                        id:44,
                        label:"学生信息"
                    },{
                        id:45,
                        label:"Mini结果"
                    },{
                        id: 43,
                        label: '任务管理',
                    }, {
                          id: 46,
                          label: '文章管理',
                        }]
                },{
                    id: 29,
                    label: '系统管理',
                    children: [{
                        id: 5,
                        label: '高校管理',
                        children:[{
                            id: 6,
                            label: '查询高校'
                        },{
                            id: 7,
                            label: '新增高校'
                        },{
                            id: 8,
                            label: '编辑高校'
                        },{
                            id: 9,
                            label: '删除高校'
                        }]
                    }, {
                        id: 10,
                        label: '学院管理',
                        children:[{
                            id: 11,
                            label: '查询学院'
                        },{
                            id: 12,
                            label: '新增学院'
                        },{
                            id: 13,
                            label: '编辑学院'
                        },{
                            id: 14,
                            label: '删除学院'
                        }]
                    }, {
                        id: 15,
                        label: '中心管理',
                        children:[{
                            id: 16,
                            label: '查询中心'
                        },{
                            id: 17,
                            label: '新增中心'
                        },{
                            id: 18,
                            label: '编辑中心'
                        },{
                            id: 19,
                            label: '删除中心'
                        }]
                    },
                        // {
                        // id: 30,
                        // label: '公告管理',
                        // children:[{
                        //     id: 31,
                        //     label: '查询图文'
                        // },{
                        //     id: 32,
                        //     label: '新增图文'
                        // },{
                        //     id: 33,
                        //     label: '编辑图文'
                        // },{
                        //     id: 34,
                        //     label: '删除图文'
                        // }]},
                        {
                        id: 35,
                        label: '角色管理',
                        children:[{
                            id: 36,
                            label: '新增角色'
                        },{
                            id: 37,
                            label: '编辑角色'
                        },{
                            id: 38,
                            label: '删除角色'
                        }]
                    },{
                        id: 39,
                        label: '用户管理',
                        children:[{
                            id: 40,
                            label: '新增用户'
                        },{
                            id: 41,
                            label: '编辑用户'
                        },{
                            id: 42,
                            label: '删除用户'
                        }]
                    }]
                }],
                //data2是给学校管理员看的
                data2: [
                    {
                    id: 4,
                    label: '数据报表',
                    children: [{
                        id: 47,
                        label: '生源地区分布',
                    },{
                        id: 48,
                        label: '学院人数分布',
                    },{
                        id: 49,
                        label: '评测数据统计',
                    },{
                        id: 50,
                        label: '问题学生分布',
                    }]
                }, {
                    id: 25,
                    label: '心理健康中心',
                    children: [{
                        id: 26,
                        label: '量表管理',
                        children:[{
                                id: 27,
                                label: '查询量表'
                            },
                            {
                                id: 28,
                                label: '删除量表'
                            }]
                    },{
                        id: 20,
                        label: '学生管理',
                        children:[{
                            id: 21,
                            label: '查询学生'
                        },{
                            id: 22,
                            label: '新增学生'
                        },{
                            id: 23,
                            label: '编辑学生'
                        },{
                            id: 24,
                            label: '删除学生'
                        }]
                    }, {
                        id:44,
                        label:"学生信息"
                    },{
                        id:45,
                        label:"Mini结果"
                    },{
                        id: 43,
                        label: '任务管理',
                    }, {
                          id: 46,
                          label: '文章管理',
                        }]
                },{
                    id: 29,
                    label: '系统管理',
                    children: [{
                        id: 5,
                        label: '高校管理',
                        children:[{
                            id: 6,
                            label: '查询高校'
                        },{
                            id: 7,
                            label: '新增高校'
                        },{
                            id: 8,
                            label: '编辑高校'
                        },{
                            id: 9,
                            label: '删除高校'
                        }]
                    }, {
                        id: 10,
                        label: '学院管理',
                        children:[{
                            id: 11,
                            label: '查询学院'
                        },{
                            id: 12,
                            label: '新增学院'
                        },{
                            id: 13,
                            label: '编辑学院'
                        },{
                            id: 14,
                            label: '删除学院'
                        }]
                    }, {
                        id: 15,
                        label: '中心管理',
                        children:[{
                            id: 16,
                            label: '查询中心'
                        },{
                            id: 17,
                            label: '新增中心'
                        },{
                            id: 18,
                            label: '编辑中心'
                        },{
                            id: 19,
                            label: '删除中心'
                        }]
                    },
                        // {
                        // id: 30,
                        // label: '公告管理',
                        // children:[{
                        //     id: 31,
                        //     label: '查询图文'
                        // },{
                        //     id: 32,
                        //     label: '新增图文'
                        // },{
                        //     id: 33,
                        //     label: '编辑图文'
                        // },{
                        //     id: 34,
                        //     label: '删除图文'
                        // }]},
                        {
                        id: 35,
                        label: '角色管理',
                        children:[{
                            id: 36,
                            label: '新增角色'
                        },{
                            id: 37,
                            label: '编辑角色'
                        },{
                            id: 38,
                            label: '删除角色'
                        }]
                    },{
                        id: 39,
                        label: '用户管理',
                        children:[{
                            id: 40,
                            label: '新增用户'
                        },{
                            id: 41,
                            label: '编辑用户'
                        },{
                            id: 42,
                            label: '删除用户'
                        }]
                    }]
                }],
                defaultProps: {
                    children: 'children',
                    label: 'label'
                },
                addInfo:{
                    name:"",
                    remark:"",
                    uid:null,
                    limit1:[],
                    limit2:[],
                },
                editInfo:{
                    id:null,
                    name:"",
                    remark:"",
                    uid:null,
                    limit1:[],
                    limit2:[],
                },
                showInfo:{
                    limit1:[],
                    limit2:[],
                },
                uOptions:[],
                info:{},
                dialogFormVisible:false,
                dialogFormVisible2:false,
                condition1:true,
                condition2:true,
                uLabel:1
            };
        }
    }
</script>

<style scoped>

</style>
